package org.example.exception;


import lombok.extern.slf4j.Slf4j;
import org.example.common.BaseResponse;
import org.example.common.ErrorCode;
import org.example.common.ResultUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e){
        log.error("businessException:"+e.getMessage(),e);
        BaseResponse error = ResultUtils.error(e.getCode(), e.getMessage(), e.getDescription());
        System.out.println(error);
        return error;
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeExceptionHandler(BusinessException e){
        log.error("runtimeException",e);
        BaseResponse error = ResultUtils.error(ErrorCode.SYSTEM_ERROR, e.getMessage(), "");
        System.out.println(error);
        return error;
    }
}
